# Copyright (C) 2002-2004, 2006, 2009 The Written Word, Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# $Id: sh.py 748 2010-11-02 11:25:38Z gary $

import os
import sys

import misclib
import prog

from prog.archive import Archive

class Archive (Archive):
  def unpack (self, dry_run, verbose, callback, dest = None,
  filelist = None, list_files = False):
    if not dry_run:
      self.verify_md5_sig ()

    self.environ['SHELL'] = self.bash_path

    if verbose:
      if dry_run:
        callback ('msg', '$ ' + self.bash_path + ' ' +
                  os.path.normpath (self.path) + '\n', 2)
      else:
        if os.spawnve (os.P_WAIT, self.bash_path, [self.bash_path,
                                                   '--noprofile', '--norc',
                                                   self.path],
                       self.environ):
          prog.ProgArchiveError ('error executing patch script ' +
                                 os.path.normpath (self.path))
    else:
      if dry_run:
        callback ('msg', '$ ' + self.bash_path + ' ' +
                  os.path.normpath (self.path) + ' >/dev/null\n', 2)
      else:
        if os.spawnve (os.P_WAIT, self.bash_path, [self.bash_path,
                                                   '--noprofile', '--norc',
                                                   self.path],
                       self.environ):
          prog.ProgArchiveError ('error executing patch script ' +
                                 os.path.normpath (self.path))
